package com.scheduled;

import com.gavel.utils.StringUtils;
import com.mrobuy.jdvc.dao.CommonDao;
import com.mrobuy.jdvc.entity.VCShelvesItem;
import com.mrobuy.jdvc.entity.VCShelvesTask;
import com.mrobuy.jdvc.service.ShelvesService;
import com.mrobuy.type.ItemState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 上架sku预处理
 */
@Component
public class ShelvesItemPrehandlTask {

    private static Logger LOG = LoggerFactory.getLogger(ShelvesItemPrehandlTask.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private CommonDao commonDao;

    @Scheduled(initialDelay =  1000, fixedDelay = 1000 * 60 * 5)
    public void reportCurrentTime() {

        if ( !TaskSwitch.ITEM_PREHANDL.get() ) {
            return;
        }

        List<VCShelvesTask> tasks = jdbcTemplate.query("select  * from vc_shelves_task where ENABLE = 'Y' order by ENABLETIME", new BeanPropertyRowMapper<>(VCShelvesTask.class));
        if (tasks == null || tasks.size() == 0) {
            return;
        }

        for (VCShelvesTask task : tasks) {
            try {
                LOG.info("[SKU上架预处理][Task: {}]{}", task.getId(), task.getName());
                prehandl(task);
            } catch (Exception e) {
                LOG.error(e.getMessage(), e.getCause());
            }
        }

    }

    private void prehandl(VCShelvesTask task) {
        if (task == null || StringUtils.isBlank(task.getId())) {
            return;
        }

        List<VCShelvesItem> items = jdbcTemplate.query("select  * from VC_SHELVES_ITEM where TASKID = ?  and ZT = ? order by PRODUCTCODE", new BeanPropertyRowMapper<>(VCShelvesItem.class), task.getId(), ItemState.INIT.code());
        if (items == null || items.size() == 0) {
            return;
        }

        Map<String, List<VCShelvesItem>> map = items.stream().collect(Collectors.groupingBy(VCShelvesItem::getProductcode));

        for (Map.Entry<String, List<VCShelvesItem>> entry : map.entrySet()) {
            LOG.info("\t[上架预处理]{}: {}", entry.getKey(), entry.getValue().size());
            for (VCShelvesItem shelvesItem : entry.getValue()) {
                {
                    // 商品名称
                    String productName =  shelvesItem.getProductname();
                    if ( StringUtils.isNotBlank(shelvesItem.getBrand()) ) {
                        productName = StringUtils.replaceIgnoreCase(productName, shelvesItem.getBrand().trim(), "");
                    }
                    if ( StringUtils.isNotBlank(shelvesItem.getBranden()) ) {
                        productName = StringUtils.replaceIgnoreCase(productName, shelvesItem.getBranden().trim(), "");
                    }

                    if ( StringUtils.isNotBlank(shelvesItem.getBrandName()) ) {
                        productName = shelvesItem.getBrandName().trim() + " " +  productName;
                    }
                    shelvesItem.setJdproductname(productName);
                }

                {


                    // SKU 名称
                    // ：品牌 + 型号 + 描述部分 + 交货
                    // 京苏 BST1148 环保可回收标识 【预计14天出货】
                    String skuName =  shelvesItem.getSkuname();
                    if ( StringUtils.isNotBlank(shelvesItem.getBrand()) ) {
                        skuName = StringUtils.replaceIgnoreCase(skuName, shelvesItem.getBrand().trim(), "");
                    }
                    if ( StringUtils.isNotBlank(shelvesItem.getBranden()) ) {
                        skuName = StringUtils.replaceIgnoreCase(skuName, shelvesItem.getBranden().trim(), "");
                    }

                    if ( StringUtils.isNotBlank(shelvesItem.getModel()) ) {
                        skuName = StringUtils.replaceIgnoreCase(skuName, shelvesItem.getModel().trim(), "");
                    }

//
//                    if ( StringUtils.isNotBlank(shelvesItem.getDim1Val()) ) {
//                        if ( !skuName.toLowerCase().contains(shelvesItem.getDim1Val().toLowerCase().trim()) ) {
//                            String tempName = skuName + " " +  shelvesItem.getDim1Val().trim();
//                            if ( tempName.length() <= 50 ) {
//                                skuName = tempName;
//                            }
//                        }
//
//                    }
//
//                    if ( StringUtils.isNotBlank(shelvesItem.getDim2Val()) ) {
//                        if ( !skuName.toLowerCase().contains(shelvesItem.getDim2Val().toLowerCase().trim()) ) {
//                            String tempName = skuName + " " +  shelvesItem.getDim2Val().trim();
//                            if ( tempName.length() <= 50 ) {
//                                skuName = tempName;
//                            }
//                        }
//                    }

                    String delivery = shelvesItem.getDeliverydate();

                    if (  "直送产品".equalsIgnoreCase(delivery)  ) {
                        delivery = "预计当日出货";
                    } else  if (  "当日出货".equalsIgnoreCase(delivery)  ) {
                        delivery = "预计当日出货";
                    } else if (  "暂时缺货".equalsIgnoreCase(delivery)  ) {
                        shelvesItem.setEnable("N");
                    }  else if (  "停止销售".equalsIgnoreCase(delivery)  ) {
                        shelvesItem.setEnable("N");
                    }  else  {
                        delivery = "预计" + delivery + "出货";
                    }

                    // 品牌 + 型号 + 描述部分 + 交货
                    shelvesItem.setJdskuname(String.format("%s %s %s 【%s】", shelvesItem.getBrandName().trim(), shelvesItem.getModel() ,  skuName.trim(), delivery));
                }

                {
                    // 产地
                    if ( StringUtils.isBlank(shelvesItem.getOriginalPlace()) ) {
                        shelvesItem.setOriginalPlace("中国大陆");
                    }
                }

                {
                    // 质保期
                    if ( StringUtils.isBlank(shelvesItem.getWarranty()) ) {
                        shelvesItem.setWarranty("3个月质保");
                    }
                }

                shelvesItem.setZt(ItemState.READY.code());
                commonDao.update(shelvesItem);
            }
        }

    }
}
